#!/bin/bash
# config env
source openrc.sh
echo "$CONTROLLER_IP $CONTROLLER_HOST_NAME" >> /etc/hosts
# config network,firewall
systemctl stop firewalld >> /dev/null 2>&1
systemctl disable firewalld >> /dev/null 2>&1
systemctl stop NetworkManager >> /dev/null 2>&1
systemctl disable NetworkManager >> /dev/null 2>&1
sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
setenforce 0
yum remove -y NetworkManager firewalld
systemctl restart network
echo "nameserver 114.114.114.114" > /etc/resolv.conf
if [[ `ip a |grep -w $CONTROLLER_IP ` != '' ]];then
        hostnamectl set-hostname $CONTROLLER_HOST_NAME
	ETCD_IP=$CONTROLLER_IP
elif [[ `ip a |grep -w $COMPUTE_IP ` != '' ]];then
        hostnamectl set-hostname $COMPUTE_HOST_NAME
	ETCD_IP=$COMPUTE_IP
else
        hostnamectl set-hostname $CONTROLLER_HOST_NAME
	ETCD_IP=$CONTROLLER_IP
fi
# config yum
curl http://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/CentOS-Base.repo
yum makecache
# ntp
yum install -y chrony
if [ 0  -ne  $? ]; then
	echo -e "\033[31mThe installation source configuration errors\033[0m"
	exit 1
fi
sed -i 's/^server/#&/' /etc/chrony.conf
echo "server $CONTROLLER_HOST_NAME iburst" >> /etc/chrony.conf
echo "allow 0.0.0.0" >> /etc/chrony.conf
systemctl enable chronyd.service
systemctl start chronyd.service
# openstack packages
yum install centos-release-openstack-train -y
yum install python-openstackclient openstack-selinux -y
# Message queue
yum install -y rabbitmq-server
systemctl enable rabbitmq-server.service
systemctl restart rabbitmq-server.service
rabbitmqctl add_user $RABBIT_USER $RABBIT_PASS
rabbitmqctl set_permissions $RABBIT_USER ".*" ".*" ".*"
# Memcached
yum install memcached python-memcached -y
sed -i 's/^OPTIONS=""/OPTIONS="-l 127.0.0.1,::1,$CONTROLLER_HOST_NAME"/g' /etc/sysconfig/memcached 
systemctl enable memcached.service
systemctl start memcached.service
# Etcd
yum install -y etcd crudini
sed -i 's/^#\[Member\]/\[Member\]/' /etc/etcd/etcd.conf
sed -i 's/^#\[Clustering\]/\[Clustering\]/' /etc/etcd/etcd.conf
crudini --set  /etc/etcd/etcd.conf Member ETCD_LISTEN_PEER_URLS http://$ETCD_IP:2380
crudini --set  /etc/etcd/etcd.conf Member ETCD_LISTEN_CLIENT_URLS http://$ETCD_IP:2379
crudini --set  /etc/etcd/etcd.conf Member ETCD_NAME $HOSTNAME
crudini --set  /etc/etcd/etcd.conf Clustering ETCD_INITIAL_ADVERTISE_PEER_URLS http://$ETCD_IP:2380
crudini --set  /etc/etcd/etcd.conf Clustering ETCD_ADVERTISE_CLIENT_URLS http://$ETCD_IP:2379
crudini --set  /etc/etcd/etcd.conf Clustering ETCD_INITIAL_CLUSTER "$HOSTNAME=http://$ETCD_IP:2380"
crudini --set  /etc/etcd/etcd.conf Clustering ETCD_INITIAL_CLUSTER_TOKEN etcd-cluster-01
crudini --set  /etc/etcd/etcd.conf Clustering ETCD_INITIAL_CLUSTER_STATE new
systemctl enable etcd
systemctl start etcd
